/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  NfcNppExample.
*/

#ifndef NFCNPPEXAMPLE_H
#define NFCNPPEXAMPLE_H

/*! \file
 *  \brief NfcNppExample creates a peer-to-peer connection between two
 *  NFC enabled devices and demonstrates how to send and receive NDEF messages
 *  between the devices using NPP protocol.
 */

#include <QObject>
#include <QList>

#include <qmobilityglobal.h>
#include <qllcpsocket.h>
#include <qndefmessage.h>

QTM_BEGIN_NAMESPACE
class QNearFieldManager;
class QNearFieldTarget;
class QLlcpServer;
QTM_END_NAMESPACE

QTM_USE_NAMESPACE // Use Qt Mobility namespace

/*!
 *  \brief  
 *   NfcNppExample class is used to create peer-to-peer connection between
 *   between two NFC enabled devices. It notifies UI of successful connect 
 *   event, when the data is sent to other device and constructs and deconstructs
 *   the sent and received NDEF messages using NPP format.
 */
class NfcNppExample : public QObject
{
    Q_OBJECT

public:

    //! Constructor.
    /*!
     *  \param parent Parent for the object.
     */
    explicit NfcNppExample(QObject *parent = 0);

    //! Desctructor.
    /*!
     */
    virtual ~NfcNppExample();

Q_SIGNALS:

    //! Signal to close active client connection.
    /*!
     *  Data has been written to remote device and the connection
     *  can be closed. Signal is emitted to close connection.     
     */     
    void closeConnection();
    
    //! Signal to notify UI, that a new NDEF message has been received.
    /*!
     *  Received NDEF Push Protocol message is parsed before the content data
     *  is extracted from the NFC record included in the NDEF message.
     *
     * \param type Type of the NFC record. "U" for the NFC URI record and "T"
     *             for the NFC Text record.
     * \param messageData Extracted NFC record data in string format.
     *
     */
    void messageReceived(const QString &type, const QString &messageData);

    //! Signal to notify UI, that the LLCP connection between devices has been succesfully created.
    /*!
     *  Signal is emitted when the client socket provided by the LLCP server or the
     *  server socket has connected succesfully.
     */
    void targetConnected();

public Q_SLOTS:

    //! Sends a message
    /*!
     *  Sends data to a remote device.
     *
     * \param type Type of the NFC record. "U" for the NFC URI record and "T"
     *             for the NFC Text record.
     * \param text NFC record data in string format.
     */
    void sendMessage(const QString &type, const QString &text);

private Q_SLOTS:

    //! Close the active client connection.
    /*!
     *  Close the active connection.
     */
    void handleCloseConnection();
    
    //! Read message data.
    /*!
     *  Reads data from the socket, that the remote device has sent.
     */
    void readMessage();

    //! Called when an NFC enabled device is in range.
    /*!
     *  This slot is called whenever a target is detected. The \a target parameter
     *  represents the detected target. Target is checked first, whether it supports
     *  LLCP access.
     *
     *  \param target Detected target.
     */
    void handleTargetDetected(QNearFieldTarget *target);

    //! Called when NFC enabled device is out of range.
    /*!
     *  This slot is called whenever a target moves out of proximity. The \a target
     *  parameter represents the lost target.
     *
     *  \param target Lost target.
     */
    void handleTargetLost(QNearFieldTarget *target);

    //! Handles new connection.
    /*!
     *  Creates a LLCP socket connection to remote device.
     */
    void handleNewConnection();

    //! Parse NDEF Push Protocol (NPP) message.
    /*!
     *  NDEF messages are extracted from the NPP message.
     *
     *  \param array NPP message as binary data.
     */
    QList<QNdefMessage> parseNppMessage(const QByteArray &array) const;

    //! Create NDEF Push Protocol (NPP) message.
    /*!
     *  Creates an NPP message from the NDEF messages contained in the \a messages parameter.
     *
     *  \param messages List of NDEF messages to be using NPP protocol.
     *  \return NPP message in binary data format.
     */
    QByteArray createNppMessage(const QList<QNdefMessage> &messages) const;

    //! Handle socket state changes.
    /*!
     *  This slot is called whenever the state of the client or the server socket changes.
     *  Mainly the ConnectedState and UnconnectedState states are handled to emit the
     *  corresponding signals to the UI.
     *
     *  \param socketState State of the socket.
     *  \sa targetConnected() targetDisconnected()
     */
    void handleSocketStateChanged(QLlcpSocket::SocketState socketState);

private:
     /*! \brief Pointer to the QNearFieldManager class instance, owned. */
    QNearFieldManager *nfcManager;
    
    /*! \brief Pointer to the QLlcpServer class instance, owned. */
    QLlcpServer *nfcServer;
    
    /*! \brief Pointer to the QLlcpSocket class instance, owned. */
    QLlcpSocket *nfcClientSocket;
    
    /*! \brief Pointer to the QLlcpSocket class instance provided by the QLlcpServer instance, owned. */
    QLlcpSocket *nfcServerSocket;

    /*! \brief Message buffer, that is sent. */
    QByteArray ndefMessage;
};

#endif // NFCNPPEXAMPLE_H
